//
//  SPRegisiterViewController.m
//  Sport
//
//  Created by _LJX on 2018/5/8.
//  Copyright © 2018年 Cass. All rights reserved.
//

#import "SPRegisiterViewController.h"

@interface SPRegisiterViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneNumberTextfiled;
@property (weak, nonatomic) IBOutlet UITextField *verificationCodeTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordAgainTextfield;
@property (weak, nonatomic) IBOutlet UIButton *VerificationCodeBtn;
@property (weak, nonatomic) IBOutlet UIButton *confirmBtn;


@property (strong, nonatomic)UIAlertController *alertPhone;
@property (strong, nonatomic)UIAlertController *alertPasswordLength;
@property (strong, nonatomic)UIAlertController *alertPasswordAgain;
@property (strong, nonatomic)UIAlertController *alertVerificationCode;
@property(copy, nonatomic)NSString *phoneNumber;
@property (copy, nonatomic)NSString *verificationCode;
@property (nonatomic,assign) BOOL isVerification;
@property (nonatomic,assign) BOOL isPassword;
@property (nonatomic,assign) BOOL isAgainPassword;

@end

@implementation SPRegisiterViewController

-(UIAlertController *)alertPhone{
    if (_alertPhone == nil) {
        _alertPhone = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入完整的电话号码" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleCancel handler:nil];
        [_alertPhone addAction:action];
    }
    return _alertPhone;
}

-(UIAlertController *)alertPasswordLength{
    if (_alertPasswordLength == nil) {
        _alertPasswordLength = [UIAlertController alertControllerWithTitle:@"提示" message:@"密码须大于6位" preferredStyle:UIAlertControllerStyleAlert];
         UIAlertAction *action = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleCancel handler:nil];
        [_alertPasswordLength addAction:action];
    }
    return _alertPasswordLength;
}

-(UIAlertController *)alertPasswordAgain{
    if (_alertPasswordAgain == nil) {
        _alertPasswordAgain = [UIAlertController alertControllerWithTitle:@"提示" message:@"两次密码不相同" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleCancel handler:nil];
        [_alertPasswordAgain addAction:action];
    }
    return _alertPasswordAgain;
}

-(UIAlertController *)alertVerificationCode{
    if(_alertVerificationCode == nil){
        _alertVerificationCode = [UIAlertController alertControllerWithTitle:@"提示" message:@"验证码错误" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleCancel handler:nil];
        [_alertVerificationCode addAction:action];
    }
    return _alertVerificationCode;
}

-(void)sendVerificationCodeWithPhone:(NSString *)phone{
    NSString *url = [NSString stringWithFormat:@"http://120.78.186.122/JT/registerPhone.php"];
    NSDictionary *dict = @{
                           @"phoneNum":phone
                           };
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json",@"text/json",@"text/javascript"@"text/plain", nil];
    [manager POST:url parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        self.verificationCode = [NSString stringWithFormat:@"%@",responseObject[@"error"]];
        NSLog(@"--验证码--%@",self.verificationCode);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"失败");
    }];
}

//点击发送验证码
- (IBAction)sendCode:(id)sender {
    if ([_phoneNumberTextfiled.text length]>9) {
        _phoneNumber  =  _phoneNumberTextfiled.text;
        [self sendVerificationCodeWithPhone:_phoneNumber];
    }else{
        [self presentViewController:_alertPhone animated:YES completion:nil];
    }
}

- (IBAction)confirm:(id)sender {
    if (_verificationCode!=nil&& [_verificationCode isEqualToString:_verificationCodeTextField.text]){
        self.isVerification = YES;
    }
    else{
        [self presentViewController:_alertVerificationCode animated:YES completion:nil];
        return;
    }
    if (_passwordTextField.text.length>5) {
        self.isPassword = YES;
    }else{
        [self presentViewController:_alertPasswordLength animated:YES completion:nil];
        return;
    }
    if ([_passwordAgainTextfield.text isEqualToString:_passwordTextField.text]) {
        self.isAgainPassword = YES;
    }else{
        [self presentViewController:_alertPasswordAgain animated:YES completion:nil];
        return;
    }
    NSLog(@"验证成功");
    if (self.isVerification == YES && self.isAgainPassword == YES && self.isPassword == YES) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    //执行注册
}

- (IBAction)back:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.passwordTextField.secureTextEntry = YES;
    self.passwordAgainTextfield.secureTextEntry = YES;
    [self alertVerificationCode];
    [self alertPasswordAgain];
    [self alertPasswordLength];
    [self alertPhone];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
